package com.mxpio.erp.technology.service.impl;

import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.technology.entity.*;
import com.mxpio.erp.common.technology.service.ProdTecRoutService;
import com.mxpio.erp.common.technology.vo.VersionVO;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ProdTecRoutServiceImpl extends BaseServiceImpl<ProdTecRout> implements ProdTecRoutService {
    @Override
    @Transactional
    public ProdTecRout listRout(String productItemCode, VersionVO vo) {
        List<Item> itemList = JpaUtil.linq(Item.class).equal("itemCode",productItemCode).equal("itemSource","1").list();
        if(CollectionUtils.isEmpty(itemList)){
            return new ProdTecRout();
        }
        Item item = itemList.get(0);
        List<ProdTecRout> bomInfoList;
        if (vo.getDate() != null) {
            bomInfoList = JpaUtil.linq(ProdTecRout.class).equal("productItemCode", productItemCode).lessThanOrEqualTo("beginTime", vo.getDate()).greaterThan("endTime", vo.getDate()).list();
        } else if (StringUtils.isNotBlank(vo.getVersion())) {
            bomInfoList = JpaUtil.linq(ProdTecRout.class).equal("productItemCode", productItemCode).equal("version", vo.getVersion()).list();
        } else {
            bomInfoList = JpaUtil.linq(ProdTecRout.class).equal("productItemCode", productItemCode).equal("primaryRout", "1").list();
        }

        ProdTecRout tecRout;
        if (CollectionUtils.isEmpty(bomInfoList)) {
            tecRout = new ProdTecRout();
            tecRout.setProductItemCode(item.getItemCode());
            tecRout.setProductItemName(item.getItemName());
            tecRout.setItemSpec(item.getItemSpec());
            tecRout.setProductDrawingNo(item.getDrawingNo());
        } else {
            tecRout = bomInfoList.get(0);
            List<ProdTecRoutProc> lineList = JpaUtil.linq(ProdTecRoutProc.class)
                    .collect("processCode", ProcessInfo.class,"processCode")
                    .equal("routId", tecRout.getProductItemCode())
                    .lessThanOrEqualTo("beginTime", tecRout.getBeginTime())
                    .greaterThan("endTime", tecRout.getBeginTime()).asc("processOrder").list();
            if (lineList.size()>0){
                lineList.forEach(per->per.setSaveTransient(false));
            }
            tecRout.setLineList(lineList);
        }
        return tecRout;
    }

    @Override
    @Transactional
    public List<ProdTecRout> listHistory(String productItemCode) {
        List<ProdTecRout> routList = JpaUtil.linq(ProdTecRout.class).equal("productItemCode", productItemCode).asc("beginTime").list();
        if (CollectionUtils.isEmpty(routList)) {
            return routList;
        }
        List<ProdTecRoutProc> bomInfoLineList = JpaUtil.linq(ProdTecRoutProc.class).equal("routId", productItemCode).list();
        if (CollectionUtils.isEmpty(bomInfoLineList)) {
            return routList;
        }

        for (ProdTecRout bomInfo : routList) {
            Date bomBeginTime = bomInfo.getBeginTime();
            if (bomInfo.getLineList() == null) {
                bomInfo.setLineList(new ArrayList<>());
            }
            for (ProdTecRoutProc line : bomInfoLineList) {
                if (line.getBeginTime().compareTo(bomBeginTime) <= 0 && line.getEndTime().compareTo(bomBeginTime) >= 0) {
                    bomInfo.getLineList().add(line);
                }
            }
        }
        return routList;
    }

    @Override
    @Transactional
    public Page<Item> getUnrelatedProdPage(Pageable pageAble, Criteria criteria) {
        return JpaUtil
                .linq(Item.class)
                .notExists(ProdTecRout.class)
                    .equalProperty("productItemCode", "itemCode")
                .end()
                .notExists(ProdTecRoutTmp.class)
                    .equalProperty("productItemCode", "itemCode")
                .end()
                .notExists(ProdTecRoutAudit.class)
                    .equalProperty("productItemCode", "itemCode")
                    .notEqual("useType", OrderStatusEnums.BomStatus.VOIDED.getStatusCode())
                .end()
                .equal("itemSource", "1")
                .where(criteria)
                .paging(pageAble);
    }
}
